﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public class MapBlockData
{
    public static MapBlockData Inst { get; set; }

    public Dictionary<int, MapStaticBlock> Blocks = new Dictionary<int, MapStaticBlock>();
    
    public MapBlockData(string strPath)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(strPath);
        foreach (XmlNode node in xmldoc.FirstChild.ChildNodes)
        {
            if (node.Name == "Block")
            {
                MapStaticBlock newBlock = new MapStaticBlock(node as XmlElement);
                this.Blocks[newBlock.Id] = newBlock;
            }
            Inst = this;
        }
    }

    public MapBlockData()
    {
       
    }

    public void Load()
    {
       
    }
}

public class MapStaticBlock
{
    public int Id;
    public string Name;
    public string PrefabPath;
    public int PhysicalType;

    public int Width = 1;
    public int Heigth = 1;
    public MapStaticBlock(XmlElement elem)
    {
        // <Block Id="1" Name="Brick" PrefabPath="Brick" PhysicalType="0">
        this.Id = int.Parse(elem.Attributes["Id"].Value);
        this.Name = elem.Attributes["Name"].Value;
        this.PrefabPath = elem.Attributes["PrefabPath"].Value;
        this.PhysicalType = int.Parse(elem.Attributes["PhysicalType"].Value);
        if (elem.Attributes["Width"] != null)
        {
            this.Width = int.Parse(elem.Attributes["Width"].Value);
        }
        if (elem.Attributes["Height"] != null)
        {
            this.Heigth = int.Parse(elem.Attributes["Height"].Value);
        }
    }

    public void EachBlock(System.Action<int, int> fn)
    {
        for (int yi = 0; yi < this.Heigth; yi++)
        {
            for (int xi = 0; xi < this.Width; xi++)
            {
                if (fn != null) fn(xi, yi);
            }
        }
    }
}

public class MapBlock
{
    public int StaticBlockIndex;
    public MapStaticBlock StaticBlock
    {
        get
        {
            return MapBlockData.Inst.Blocks[StaticBlockIndex];
        }
    }
    public int PosX;
    public int PosY;
    public MapBlock(int staticBlockIndex, int posX, int posY)
    {
        this.StaticBlockIndex = staticBlockIndex;
        this.PosX = posX;
        this.PosY = posY;
    }
}
